<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="200px">
      <el-form-item label="招生年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入招生年份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学科名" prop="subId">
        <el-select v-model="queryParams.subId" clearable filterable>
          <el-option v-for="subject in subjectList" :label="subject.subName" :value="subject.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="招生名额数" prop="pgNumber">
        <el-input
          v-model="queryParams.pgNumber"
          placeholder="招生名额数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: 100px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mentor:spot:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mentor:spot:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mentor:spot:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mentor:spot:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spotList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="招生年份" align="center" prop="year" />
      <el-table-column label="导师" align="center" >
        <template>
          <el-tag type="primary">{{ins.insName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="学科名" align="center" width="180">
        <template slot-scope="scope">
          <el-tag type="warning">{{subjectMap[scope.row.subId]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="已招生人数" align="center" prop="checkedNumber" />
      <el-table-column label="招生名额数" align="center" prop="pgNumber" />
      <el-table-column label="审核状态" align="center" prop="examineStatus" width="120" >
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.examineStatus==0">未审核</el-tag>
          <el-tag v-if="scope.row.examineStatus==1">审核中</el-tag>
          <el-tag type="danger" v-if="scope.row.examineStatus==2">审核未通过</el-tag>
          <el-tag type="success" v-if="scope.row.examineStatus==3">审核通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mentor:spot:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mentor:spot:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改招生名额对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="招生年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入招生年份" />
        </el-form-item>
        <el-form-item label="学科名" prop="subId" label-width="100px">
          <el-select v-model="form.subId" clearable filterable>
            <el-option v-for="subject in subjectList" :label="subject.subName" :value="subject.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.id" label="已招生人数" prop="checkedNumber" label-width="100px">
          <el-input-number v-model="form.checkedNumber" :min="0" placeholder="已招生人数" />
        </el-form-item>

        <el-form-item label="招生名额数" prop="pgNumber" label-width="100px">
          <el-input-number v-model="form.pgNumber" :min="1" placeholder="招生名额数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listSpot, getSpot, delSpot, addSpot, updateSpot, exportSpot } from "@/api/mentor/spot";
  import {getInfo} from "@/api/login";
  import {getInstructorDetailByUserId} from '@/api/mentor/instructor'
  import {getAllSubject, getAllSubjectMap} from "../../../api/mentor/subject";
  export default {
    name: "Spot",
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 招生名额表格数据
        spotList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          year: undefined,
          insId:undefined,
          subId: undefined,
          pgNumber: undefined,
          examineStatus:undefined
        },
        // 表单参数
        form: {
          examineStatus:1
        },
        // 表单校验
        rules: {
          year: [
            { required: true, message: "招生年份不能为空", trigger: "blur" }
          ],
          insId: [
            { required: true, message: "导师ID不能为空", trigger: "blur" }
          ],
          subId: [
            { required: true, message: "学科点编号不能为空", trigger: "blur" }
          ]
        },
        ins: {},
        subjectMap:{},
        subjectList:[],
      };
    },
    async created() {

      await this.getInstructorInfo()
      var flag = this.checkUser(this.user)
      if (!flag){
        this.$message.warning("本菜单只对导师开放")
        return this.$router.push('/')
      }
      await this.getAllSubjectMap()
      await this.getList();
      await this.getAllSubject()
    },
    methods: {
      /** 查询招生名额列表 */
      async getList() {
        this.loading = true;
        listSpot(this.queryParams).then(response => {
          this.spotList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          year: undefined,
          subId: undefined,
          pgNumber: undefined,
          checkedNumber:undefined,
          examineStatus: 1
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加招生名额";

      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSpot(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改招生名额";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          this.form.insId = this.ins.insId
          this.form.examineStatus = 1
          if (valid) {
            if (this.form.id != null) {
              updateSpot(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSpot(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除招生名额编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSpot(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有招生名额数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSpot(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },
      async getInstructorInfo() {
        const info = await getInfo();
        this.user = info.user
        const res = await getInstructorDetailByUserId(info.user.userId)
        this.ins = res.data
        this.queryParams.insId = res.data.insId
        this.form.insId = res.data.insId
      },
      async getAllSubjectMap() {
        const res = await getAllSubjectMap()
        // console.log(res)
        this.subjectMap = res.data
      },
      async getAllSubject(){
        const res = await getAllSubject()
        this.subjectList = res.data
      },
      //本菜单只对导师开放
      checkUser(user){
        // console.log(user)
        for (let i = 0; i < user.roles.length; i++) {
          if (user.roles[i].roleKey=='instructor'){
            return true
          }
        }
        return false
      },
    }

  };
</script>
